package com.lykj.llw.box.ui.activity

import android.os.Bundle
import android.view.View
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.component.GlideApp
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.utils.ToastUtils
import com.lykj.lib_base.net.http.CustomResponseBodyObserver
import com.lykj.lib_base.net.http.RetrofitManager
import com.lykj.llw.box.data.bean.response.RecyclingResponse
import com.lykj.llw.box.databinding.ActRecyclingSmallAccountDetailBinding
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 * ================================================
 * 回收小号详情页
 * ================================================
 *
 * @author gosha
 * @date 2023/10/23-11:44
 */
class RecyclingSmallAccountDetailActivity : BaseActivity() {
    lateinit var binding: ActRecyclingSmallAccountDetailBinding

    var mBean: RecyclingResponse.DataDTO? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActRecyclingSmallAccountDetailBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initUI()
        initData()
        setListener()
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar
            .with(this)
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()
    }

    override fun initUI() {
        mBean = intent.getParcelableExtra("bean")

        binding.titleBar.tvTitle.text = "回收小号"
        binding.titleBar.tvRightOption.visibility = View.GONE
    }

    //回收角色
    private fun recyclingRole() {
        RetrofitManager.instance.recyclingRole("${mBean?.partId}",
            object : CustomResponseBodyObserver<ResponseBody>() {
                override fun onSubscribe(d: Disposable) {
                    addDisposables(this, d)
                }

                override fun specificHandle(responseBody: String) {
                    ToastUtils.showShort(this@RecyclingSmallAccountDetailActivity, "回收成功")
                    finish()
                }

                override fun handleException(ret: String, msg: String?) {
                    msg?.let {
                        ToastUtils.showShort(this@RecyclingSmallAccountDetailActivity, it)
                    }
                }
            })
    }

    override fun initData() {
        setUIAttr()
    }

    private fun setUIAttr() {
        GlideApp.with(this)
            .load(mBean?.gameIcon)
            .transition(DrawableTransitionOptions.withCrossFade())
            .into(binding.ivIcon)

        binding.tvGameName.text = "${mBean?.gamename}"
        binding.tvRoleName.text = "${mBean?.rolename}"
        binding.tvCustomMoney.text = "${mBean?.sumPrice}"
        binding.tvObtainPtb.text = "${mBean?.ptb}"
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }

        binding.tvBtnRecyclingNow.clickWithTrigger {
            recyclingRole()
        }
    }

    override fun onDestroy() {
        removeDisposables(this)
        super.onDestroy()
    }
}